import java.util.Stack;

class Solution394 {
     int i=0 ; 
    public String decodeString(String s) {
         int len = s.length() ;
         char[] arr = s.toCharArray() ;
         Stack<Integer> numStack = new Stack<>() ;
         Stack<String> chStack = new Stack<>() ;
         for( ; i < len ; i ++){
            char ch = arr[i] ; 
            if(ch>= '0' && ch <='9'){
                 int num = getNum(arr) ; 
                 numStack.push(num) ; 
            }else if(ch == ']'){
                int num = numStack.pop() ;
                StringBuilder ret = new StringBuilder() ;
                StringBuilder strbu = new StringBuilder() ;
                while(!chStack.peek().equals("[")){
                    strbu.insert(0 , chStack.pop()) ; 
                }
                chStack.pop() ;
                for(int j=0 ; j < num ; j ++){
                    ret.append(strbu) ; 
                }
                chStack.push(ret.toString()) ; 
            }else if(ch == '[') {
                chStack.push(ch+"") ; 
            }else {
               chStack.push(getStr(arr));
            }
         }
         StringBuilder strbu = new StringBuilder() ;
             while(!chStack.empty()){
                 strbu.insert(0 , chStack.pop()) ;
             }
         return strbu.toString() ;
    }
    private String getStr(char[] arr){
        StringBuilder strbu = new StringBuilder() ;
        while( i < arr.length && arr[i] >='a' && arr[i] <='z'){
            strbu.append(arr[i]) ; 
            i ++ ;
        }
        i -- ; 
        return strbu.toString() ;
    }
        private int getNum(char[] arr){
        StringBuilder strbu = new StringBuilder() ;
        while( i < arr.length && arr[i] >='0' && arr[i] <='9'){
            strbu.append(arr[i]) ; 
            i ++ ;
        }
        i -- ; 
        return Integer.parseInt(strbu.toString()) ; 
    }
}